﻿Imports FixSoft.CloudGround.Base
Imports FlickrNet

Public Class Provider
    Implements IProvider

    Dim Flickr As New FlickrNet.Flickr("0b8439167d4a39d8c5dc46f941af5294", "6227a1f02848c0cd")

    Public Sub Initialize() Implements FixSoft.CloudGround.Base.IProvider.Initialize
        'Do nothing
    End Sub

    Public Function GetPictures(ByVal search As String, ByVal skipLowRes As Boolean, ByVal getMaxRes As Boolean, ByVal filterKeywords As System.Collections.Generic.List(Of String), ByVal DownloadedIds() As String) As System.Collections.Generic.List(Of FixSoft.CloudGround.Base.Picture) Implements FixSoft.CloudGround.Base.IProvider.GetPictures
        Return GetPictures(search, skipLowRes, getMaxRes, filterKeywords, DownloadedIds, True)
    End Function

    Public Property PageIndex(ByVal Search As String) As Integer
        Get
            If Settings.FlickrPageIndex.ContainsKey(Search) Then
                Return Settings.FlickrPageIndex(Search)
            Else
                Me.PageIndex(Search) = 1
                Return PageIndex(Search)
            End If
        End Get
        Set(ByVal value As Integer)
            If Settings.FlickrPageIndex.ContainsKey(Search) Then
                Settings.FlickrPageIndex(Search) = value
            Else
                Settings.FlickrPageIndex.Add(Search, value)
            End If
        End Set
    End Property

    Public Function GetPictures(ByVal search As String, ByVal skipLowRes As Boolean, ByVal getMaxRes As Boolean, ByVal filterKeywords As System.Collections.Generic.List(Of String), ByVal DownloadedIds() As String, ByVal IsFirst As Boolean) As System.Collections.Generic.List(Of FixSoft.CloudGround.Base.Picture)
        Dim Op As New PhotoSearchOptions
        Op.Text = search
        Op.PerPage = 32
        Op.Extras = Op.Extras Or PhotoSearchExtras.OriginalDimensions Or PhotoSearchExtras.SmallUrl Or PhotoSearchExtras.MediumUrl Or PhotoSearchExtras.Medium640Url Or PhotoSearchExtras.LargeUrl Or PhotoSearchExtras.OriginalFormat Or PhotoSearchExtras.OriginalUrl
        Op.Page = PageIndex(search)
        Dim Pics = Flickr.PhotosSearch(Op)
        If Pics.Count = 0 Then
            If IsFirst Then
                PageIndex(search) = 1
                Op.Page = PageIndex(search)
                Pics = Flickr.PhotosSearch(Op)
            Else
                Return New List(Of Picture)
            End If
        End If
        Dim Result As New List(Of Picture)
        Dim Ss As New Drawing.Size(Windows.Forms.Screen.PrimaryScreen.Bounds.Width, Windows.Forms.Screen.PrimaryScreen.Bounds.Height)
        For Each Pic In Pics
            Dim IsDup As Boolean
            'Check downloadedIds
            For Each DId As String In DownloadedIds
                If Pic.PhotoId = DId Then
                    IsDup = True
                End If
            Next
            If IsDup Then
                Continue For
            End If
            'Check filter keywords
            For Each Tag In Pic.Tags
                If IsImageUnwanted(Tag, filterKeywords) Then
                    IsDup = True
                End If
            Next
            If IsDup Then
                Continue For
            End If
            'Check size
            Dim Url As String = FindNearestSize(Ss, Pic, skipLowRes, getMaxRes)
            If String.IsNullOrEmpty(Url) = False Then
                Dim GoodPic As New Picture
                GoodPic.Url = Url
                GoodPic.Id = "Flickr_" + Pic.PhotoId
                Result.Add(GoodPic)
            End If
        Next
        If Result.Count = 0 Then
            PageIndex(search) += 1
            Return GetPictures(search, skipLowRes, getMaxRes, filterKeywords, DownloadedIds, False)
        End If
        count = Result.Count
        Return Result
    End Function
    Private Function IsImageUnwanted(ByVal tags As String, ByVal filterKeywords As IEnumerable(Of String)) As Boolean
        Return filterKeywords.Any(AddressOf tags.Contains)
    End Function

    Dim count As Integer

    Private Function FindNearestSize(ByVal ScreenSzie As Drawing.Size, ByVal Picture As Photo, ByVal SkipWithLowRes As Boolean, ByVal GetWithMaxRes As Boolean) As String
        If Picture.OriginalFormat = Nothing Then
            ' Picture.OriginalFormat = ".jpg"
        End If
        If GetWithMaxRes = False Then
            If Picture.SmallHeight >= ScreenSzie.Height AndAlso Picture.SmallWidth >= ScreenSzie.Width Then
                Return Picture.SmallUrl
            ElseIf Picture.MediumHeight >= ScreenSzie.Height AndAlso Picture.MediumWidth >= ScreenSzie.Width Then
                Return Picture.MediumUrl
            ElseIf Picture.Medium640Height >= ScreenSzie.Height AndAlso Picture.Medium640Width >= ScreenSzie.Width Then
                Return Picture.Medium640Url
            ElseIf Picture.LargeHeight >= ScreenSzie.Height AndAlso Picture.LargeWidth >= ScreenSzie.Width Then
                Return Picture.LargeUrl
            ElseIf Picture.OriginalHeight >= ScreenSzie.Height AndAlso Picture.OriginalWidth >= ScreenSzie.Width Then
                Return Picture.OriginalUrl
            Else
                If SkipWithLowRes Then
                    Return ""
                Else
                    Return Picture.OriginalUrl
                End If
            End If
        Else
            If Picture.OriginalHeight >= ScreenSzie.Height AndAlso Picture.OriginalWidth >= ScreenSzie.Width Then
                Return Picture.OriginalUrl
            ElseIf Picture.LargeHeight >= ScreenSzie.Height AndAlso Picture.LargeWidth >= ScreenSzie.Width Then
                Return Picture.LargeUrl
            ElseIf Picture.Medium640Height >= ScreenSzie.Height AndAlso Picture.Medium640Width >= ScreenSzie.Width Then
                Return Picture.Medium640Url
            ElseIf Picture.MediumHeight >= ScreenSzie.Height AndAlso Picture.MediumWidth >= ScreenSzie.Width Then
                Return Picture.MediumUrl
            ElseIf Picture.SmallHeight >= ScreenSzie.Height AndAlso Picture.SmallWidth >= ScreenSzie.Width Then
                Return Picture.SmallUrl
            Else
                If SkipWithLowRes Then
                    Return ""
                Else
                    If String.IsNullOrEmpty(Picture.OriginalUrl) Then
                        Return Picture.OriginalUrl
                    ElseIf String.IsNullOrEmpty(Picture.LargeUrl) Then
                        Return Picture.LargeUrl
                    ElseIf String.IsNullOrEmpty(Picture.Medium640Url) Then
                        Return Picture.Medium640Url
                    ElseIf String.IsNullOrEmpty(Picture.MediumUrl) Then
                        Return Picture.MediumUrl
                    ElseIf String.IsNullOrEmpty(Picture.SmallUrl) Then
                        Return Picture.SmallUrl
                    Else
                        Return ""
                    End If
                End If
            End If
        End If
    End Function
    Public Function GetResultsCount() As Integer Implements FixSoft.CloudGround.Base.IProvider.GetResultsCount
        Return count
    End Function

    Dim Settings As Settings

    Public Sub SetSettings(ByVal Settings As FixSoft.CloudGround.Base.Settings) Implements FixSoft.CloudGround.Base.IProvider.SetSettings
        Me.Settings = Settings
    End Sub
End Class
